package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.OpenUserDetailListBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.bean.UserOpenBean;
import com.koron.css2.clientCenter.mapper.OpenUserMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 开户删除
 * @author 甄玉晴
 *
 */
public class OpenAccDelete implements ServerInterface {

	@Override
	@Transactional(rollbackFor = Exception.class)
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			OpenUserMapper mapper = factory.getMapper(OpenUserMapper.class);
			UserInfoMapper userMapper = factory.getMapper(UserInfoMapper.class);
			MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
			UserOpenBean bean = JsonUtils.objectToPojo(req.getData(), UserOpenBean.class);
			//判断状态是否为草稿，如为草稿则返回
			if(bean.getProcessState()!=null&&!bean.getProcessState().equals("")) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "状态为草稿,该过户信息不能删除", void.class);
			}

			//获取开户记录信息
			List<OpenUserDetailListBean> list = mapper.getUserOpenListInfoById(bean.getId());
			//恢复在使用的水表
			list.forEach(openUserDetailListBean -> {
				UserOpenBean userOpenMessage = mapper.getUserOpenListInfoByDetailId(openUserDetailListBean.getId());
				// 获取临时用户信息
				UserBean openUserInfo = userMapper.getUserInfoById(userOpenMessage.getUserInfoTempId(), UserBean.USERTEMP);
				msInfoMapper.updateMsInfoStatus(openUserInfo.getMeterNo(),1);
			});
			mapper.userOpenRecordDelete(bean.getId());//删除用户开户记录信息
		
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
		} catch (Exception e) {
			logger.error("开户删除失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "开户删除失败", void.class);
		}
	}

}
